﻿VisualComponent = function VisualComponent(pImageData, x, y) 
{
    var _xDistance = -1;
    var _yDistance = -1;
    var _xMoveStep = -1;
    var _yMoveStep = -1;
    var _xSpeed = 1;
    var _ySpeed = 1;

    this.engine;
    this.y = y;
    this.x = x;
    this.z = 0;
    this.rotation = 0;
    this.id = 0;

    /** The image data for this object **/
    this.imageData = pImageData;

    /** This object collided with another given object **/
    this.collide = function (visualComponent) 
    {
        var o = document.getElementById("label");
        o.value = "Col" + visualComponent.id;
    }

    /** Draw this objects current state **/
    this.draw = function () 
    {
        this.engine.context.putImageData(this.imageData, this.x, this.y);
    }

    this.moveRight = function (distance) 
    {
        if (distance > 0) 
        {
            _xMoveStep = 0;
            _yMoveStep = 0;
            _xDistance = distance;
            _yDistance = 0;
            _xSpeed = 1;
            _ySpeed = 0;
        }
    }

    this.moveLeft = function (distance) 
    {
        if (distance > 0) 
        {
            _xMoveStep = 0;
            _yMoveStep = 0;
            _xDistance = distance * -1;
            _yDistance = 0;
            _xSpeed = -1;
            _ySpeed = 0;
        }
    }

    this.moveUp = function (distance) 
    {
        if (distance > 0) 
        {
            _xMoveStep = 0;
            _yMoveStep = 0;
            _xDistance = 0;
            _yDistance = distance * -1;
            _xSpeed = 0;
            _ySpeed = -1;
        }
    }

    this.moveDown = function (distance) 
    {
        if (distance > 0) 
        {
            _xMoveStep = 0;
            _yMoveStep = 0;
            _xDistance = 0;
            _yDistance = distance;
            _xSpeed = 0;
            _ySpeed = 1;
        }
    }

    this.update = function () 
    {
        var xFinished = false;
        var yFinished = false;
        var diff = ((_xDistance < 0) ? _xDistance * -1 : _xDistance) - ((_xMoveStep < 0) ? _xMoveStep * -1 : _xMoveStep);
        
        if (diff > 0) 
        {
            _xMoveStep += _xSpeed;
            this.x = this.x + _xSpeed;
        }
        else 
        {
            _xDistance = -1;
            _xMoveStep = -1;
            xFinished = true;
        }

        diff = ((_yDistance < 0) ? _yDistance * -1 : _yDistance) - ((_yMoveStep < 0) ? _yMoveStep * -1 : _yMoveStep);
        if (diff > 0) 
        {
            _yMoveStep += _ySpeed;
            this.y = this.y + _ySpeed;
        }
        else 
        {
            _yDistance = -1;
            _yMoveStep = -1;
            yFinished = true;
        }

        return (xFinished && yFinished);
    }
}

